﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BMUpload.Utility;
namespace BMUpload
{
    public partial class FrmMain : Form
    {
        FrmUpload frmu;
        public FrmMain()
        {
            InitializeComponent();
            InitToolsBar();
            initStatus();
        }

        /// <summary>
        /// 初始化工具栏
        /// </summary>
        private void InitToolsBar()
        {
            toolbar.Items.Clear(); 
            toolbar.Top = 0;
            toolbar.ImageList= imageList1; 
            string strfunc = string.Empty;
            strfunc += "#ShowForm*上传*上传进货信息*DATABASE";
            //strfunc += "#ShowForm:2*销售上传*上传销售信息*2";
            //strfunc += "#ShowForm:3*进退上传*上传进退信息*3";
            strfunc += "#Setting*设置*设置数据库连接信息*Setting";
            strfunc += "#info*信息*设置企业信息*personal";
            strfunc += "#Relogin*注销*注销重新登录*relogin";
            strfunc += "#Exit*退出*退出*Home";

            string[] arr=strfunc.Split('#');
            for (int i = 1; i < arr.Length; i++)
            {
                string[] tbarr=arr[i].Split('*');
                ToolStripButton  tb = new ToolStripButton();
                
                tb.Text = tbarr[1];
                tb.ToolTipText = tbarr[2];
                tb.ImageKey  =tbarr[3];
                
                tb.TextAlign = ContentAlignment.BottomCenter;
                tb.TextImageRelation = TextImageRelation.ImageAboveText;
                tb.Click += new EventHandler(tb_Click);
                toolbar.Items.Add(tb);
            }
        }

        private void initStatus()
        {
            ToolStripStatusLabel st = new ToolStripStatusLabel();
            st.Text = CurrentUser.Instance.SoftType;
            statusStrip1.Items.Add(CurrentUser.Instance.SoftType);
        }
        void tb_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            if (((ToolStripButton)sender).Text.Equals("上传"))
            {
                if (frmu==null)
                {
                    frmu = new FrmUpload();
                }
                if (frmu.IsDisposed)
                {
                    frmu = new FrmUpload();
                }
                frmu.MdiParent = this;
                frmu.Show();
            }
            else if (((ToolStripButton)sender).Text.Equals("设置"))
            {

                string conns = PubConstant.GetConnectionString("ConnectionString");
                new PubConstant().SetConnString(conns); 
            }
            else if (((ToolStripButton)sender).Text.Equals("信息"))
            {
                UploadBLL p = new UploadBLL();
                
                p.SetCorpInfo(); 
                
            }
            else if (((ToolStripButton)sender).Text.Equals("注销"))
            {
                FrmLogin frm = new FrmLogin();
                frm.Relogin = true;
                frm.ShowDialog();
                if (frm.DialogResult == DialogResult.OK)
                {
                    //MsgHelper.ShowInformationMsgBox("");
                }
            }
            else if (((ToolStripButton)sender).Text.Equals("退出"))
            {

                System.Environment.Exit(0);
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.Text += CurrentUser.Instance.SoftType;
            string strcorp = AppSettings.GetValue("CorpInfo");
            if (strcorp != null)
            {
                label1.Text = strcorp;
            }
            tb_Click(toolbar.Items[0], new EventArgs());

        }

        public bool checkchildFrmExist(string childFrmName)
        { 
            foreach (Form childFrm in this.MdiChildren) 
            { 
                if (childFrm.Name.ToString() == childFrmName) 
                { 
                    if (childFrm.WindowState == FormWindowState.Minimized) 
                    { 
                        childFrm.WindowState = FormWindowState.Normal;
                    }
                    childFrm.Activate();
                    return true; 
                } 
            } 
            return false;
        }


    }
}
